﻿using System;
using System.Collections.Generic;
using System.IO.Ports;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using System.Drawing;
using System.Windows.Forms;
using System.IO;
using System.ComponentModel;
using Config;

namespace SerialportSample
{
    public partial class SerialportSampleForm : Form
    {
        #region 串口

        #endregion

        public SerialportSampleForm()//构造函数
        {
            InitializeComponent();
            //初始化下拉串口名称列表框
            //获取当前计算机串口的名称数组
            //this.skinEngine1.SkinFile = "DeepGreen.ssk";//导入皮肤
            /*开启线程*/
            this.bw.DoWork += new System.ComponentModel.DoWorkEventHandler(bwDoWork);
            this.bw.ProgressChanged += new ProgressChangedEventHandler(backgroundWorker1_ProgressChanged);
            initChart();
        }
        public void initChart()
        {
            //chart2.Series[0].Points.AddXY(1, 0); 
        }
        void bwDoWork(object sender, DoWorkEventArgs e)
        {

            this.bw.ReportProgress(0, "正在读取数据文件...");
            StreamReader sr = new StreamReader(new FileStream("test.txt ", FileMode.Open, FileAccess.Read));
            int k;
            string s;
            int len;
            for (int i = 0; i < 100; ++i)
            {
                this.bw.ReportProgress(i, "Runing!"); 
                /*
                this.Invoke((MethodInvoker)delegate
                {
                    this.toolStripProgressBar1.Value += this.toolStripProgressBar1.Step;
                });
                 * */
                System.Threading.Thread.Sleep(100);
                
            }
            this.bw.ReportProgress(100, "Complete!"); 
        }
        private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            toolStripProgressBar1.Value = e.ProgressPercentage;
           // toolStripProgressBar1.Text = e.UserState as String;
            toolStripStatusLabel1.Text = e.UserState as String;
        }

        private void SerialportSampleForm_FormClosing(object sender, FormClosingEventArgs e)
        {

            //在关闭窗体前发生，此时可以取消窗体的关闭。
            DialogResult result = MessageBox.Show("你确定", "提示",
                MessageBoxButtons.OKCancel, MessageBoxIcon.Information);
            if (result == DialogResult.OK)
            {
                e.Cancel = false;  //点击OK              
                System.Environment.Exit(System.Environment.ExitCode);
                this.Dispose();
                this.Close();
            }
            else
            {
                e.Cancel = true;
            }

        }


       

        private void 配置信息修改ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Config.ConfigForm config = new Config.ConfigForm();
            config.Show();
        }

        private void 配置文件查看ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Config.ConfigForm config = new Config.ConfigForm();
            config.Show();
        }

        private void button1_Click_1(object sender, EventArgs e)
        {
            WaveForm f2 = new WaveForm();

            f2.ShowDialog();
        }

        private void buttonOpenClose_Click(object sender, EventArgs e)
        {
            bw.RunWorkerAsync();
        }

    }
}